package showtime.framework.component.pool
{
	
	
	/** 
	 * @project DelphiFrmework
	 * @author Delphi
	 * @date 2011-6-3
	 *
	 */
	
	public class ObjectPool
	{
		private var threshold:int = 0;
		
		private var loadFactor:int = 0;
		
		private var counter:int = 0;
		
		private var pool:Vector.<Object> = null;
		
		private var currentObject:Object = null;
		
		private var objectClazz:Class = null;
		
		public function ObjectPool(objectClazz:Class, initialCapacity:int, loadFactor:int = 10)
		{
			threshold = initialCapacity;
			
			this.loadFactor = loadFactor;
			this.objectClazz = objectClazz;
			this.counter = initialCapacity;
			
			var index:int = threshold;
			
			pool = new Vector.<Object>(threshold);
			
			while (--index > -1)
			{
				pool[index] = new objectClazz();
			}
		}
		
		public function getObject():Object
		{
			if (counter > 0)
			{
				return currentObject = pool[--counter];
			}

			var index:int = loadFactor;
			
			while (--index > -1)
			{
				pool.unshift(new objectClazz());
		
			}
			
			counter = loadFactor;
			
			return getObject();
		}
		
		public function dispose(object:Object):void
		{
			pool[counter++] = object;
		}
	}
}